package com.cskaoyan.service.admin;

import com.cskaoyan.bean.admin.*;
import com.cskaoyan.mapper.admin.AdminMapper;
import com.cskaoyan.mapper.admin.PermissionMapper;
import com.cskaoyan.mapper.admin.PermissionWithSystemMapper;
import com.cskaoyan.mapper.admin.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-21
 */
@Service
public class AuthServiceImpl implements AuthService{

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    PermissionWithSystemMapper permissionWithSystemMapper;

    @Override
    public void query(InfoBean bean, Integer[] roleIds) {

        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(Arrays.asList(roleIds));
        // 根据roleIds查role表获取roleName
        List<Role> roleList = roleMapper.selectByExample(roleExample);// List<String> roleNames
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
        // 根据roleIds查permission表获取permission
        List<Permission> permissionList =
                permissionMapper.selectByExample(permissionExample);
        List<String> permissions = new ArrayList<>();
        // lambda表达式进行list的类型转换
        permissionList.forEach(item -> permissions.add(item.getPermission()));
        PermissionWithSystemExample example = new PermissionWithSystemExample();
        example.createCriteria().andPermissionIdIn(permissions);
        // 根据permission查system_permission表获取api
        List<PermissionWithSystem> permissionWithSystemList =
                permissionWithSystemMapper.selectByExample(example);
        List<String> perms = new ArrayList<>();
        permissionWithSystemList.forEach(item -> perms.add(item.getApi()));
        List<String> roles = new ArrayList<>();
        roleList.forEach(item -> roles.add(item.getName()));
        bean.setRoles(roles);
        bean.setPerms(perms);
    }
}
